package net.sxnic.item.mdata.action;

import java.io.File;
import java.util.List;

import javax.servlet.ServletContext;

import net.sxnic.item.ItemActionSupport;
import net.sxnic.item.mdata.Mdata;
import net.sxnic.item.mdata.MdataManager;
import net.sxnic.item.print.word.CyhInfo;
import net.sxnic.item.print.word.GjhzInfo;
import net.sxnic.item.print.word.KjcxInfo;
import net.sxnic.item.print.word.KjggInfo;
import net.sxnic.item.print.word.rkxInfo;
import net.sxnic.jhitem.itemannex.ItemAnnex;
import net.sxnic.jhitem.itemannex.ItemAnnexManager;
import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemclob.ItemClobManager;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itemfund.ItemFundManager;
import net.sxnic.jhitem.itemgjhzunit.ItemGjhzunit;
import net.sxnic.jhitem.itemgjhzunit.ItemGjhzunitManager;
import net.sxnic.jhitem.itemoperation.ItemOperationManager;
import net.sxnic.jhitem.itempeople.ItemPeople;
import net.sxnic.jhitem.itempeople.ItemPeopleManager;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.jhitem.itemprincipal.ItemPrincipalManager;
import net.sxnic.jhitem.itemssmb.ItemSsmb;
import net.sxnic.jhitem.itemssmb.ItemSsmbManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class Print extends ItemActionSupport {
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(Print.class);
	
	@Autowired
	private ServletContext servletContext;
	
	@Autowired
	MdataManager mdManager;
	@Autowired
	protected ItemFundManager fundManager;
	
	@Autowired
	protected ItemPrincipalManager principalManager;
	
	@Autowired
	protected ItemClobManager clobManager;
	
	@Autowired
	protected  ItemPeopleManager peopleManager;
	
	@Autowired
	protected ItemSsmbManager ssmbManager;
	@Autowired
	protected ItemAnnexManager annexManager;
	@Autowired
	protected ItemOperationManager operationManager;
	@Autowired
	protected ItemGjhzunitManager gjhzunitManager;
	private String id;
	//报告正文附件列表
	protected List<ItemAnnex> annex002List;
	protected List<ItemAnnex> annex001List;
	/**
	 * 打印临汾市科技计划项目申报表
	 * @return
	 */
	public String execute() throws Exception {

		String template = "";
		String docName ="";
		Mdata mdata = mdManager.get(id);
		annex002List = annexManager.findListByFileSort("002", id);
		//科技攻关计划
		if("001".equals(mdata.getSoperCode())|| "002".equals(mdata.getSoperCode())|| "012".equals(mdata.getSoperCode()))
		{
			template="template/kjgg.doc";
			docName="临汾市科技攻关计划项目申报书";
		}
		//科技攻关计划无报告正文
		if(("001".equals(mdata.getSoperCode())|| "002".equals(mdata.getSoperCode())|| "012".equals(mdata.getSoperCode()))&&annex002List!=null&&annex002List.size()>0)
		{
			template="template/wbgzw/kjgg.doc";
			docName="临汾市科技攻关计划项目申报书";
		}
		//软科学计划
		if("005".equals(mdata.getSoperCode()))
		{
			template="template/rkx.doc";
			docName="临汾市软科学研究项目申报书";
		}
		//软科学计划无报告正文
		if(("005".equals(mdata.getSoperCode()))&&annex002List!=null&&annex002List.size()>0)
		{
			template="template/wbgzw/rkx.doc";
			docName="临汾市软科学研究项目申报书";
		}
		//科技创新计划
		if("011".equals(mdata.getSoperCode()))
		{
			template="template/kjcx.doc";
			docName="临汾市科技创新计划项目建议书";
		}
		//科技创新计划无 报告正文
		if("011".equals(mdata.getSoperCode())&&annex002List!=null&&annex002List.size()>0)
		{
			template="template/wbgzw/kjcx.doc";
			docName="临汾市科技创新计划项目建议书";
		}

		//国际合作
		if("009".equals(mdata.getSoperCode()))
		{
			template="template/gjhz.doc";
			docName="临汾市国际合作计划项目申报书";
		}

		//产业化
		if("006".equals(mdata.getSoperCode())||"007".equals(mdata.getSoperCode())||"008".equals(mdata.getSoperCode())||"013".equals(mdata.getSoperCode()))
		{
			template="template/cyh.doc";
			docName="临汾市科技产业化环境建设计划项目申报书";
		}
		//产业化 无报告正文
		if(("006".equals(mdata.getSoperCode())||"007".equals(mdata.getSoperCode())||"008".equals(mdata.getSoperCode())||"013".equals(mdata.getSoperCode())&&annex002List!=null)&&annex002List.size()>0)
		{
			template="template/wbgzw/cyh.doc";
			docName="临汾市科技产业化环境建设计划项目申报书";
		}
 

		//获取模板文件
		template=servletContext.getRealPath(template);
		String file=String.format("res_base/print/%s_intro.doc", id);
		//获取临时文件
		file=servletContext.getRealPath(file);
		ItemFund fund = fundManager.getEntityByBaseinfoId(id);
		ItemPrincipal principal = principalManager.getEntityByBaseinfoId(id);
		ItemClob clob = clobManager.getEntityByBaseinfoId(id);
		
		List<ItemGjhzunit> gwGjhzunitList = gjhzunitManager.findGwGjhzunit(id);
		List<ItemGjhzunit> gnGjhzunitList = gjhzunitManager.findGnGjhzunit(id);
		List<ItemPeople> peopleList = peopleManager.findListByBaseinfoId(id);
		List<ItemPeople> GwList = peopleManager.findGwList(id);
		List<ItemPeople> GnList = peopleManager.findGnList(id);
		List<ItemSsmb> ssmbList= ssmbManager.findListByBaseinfoId(id);
		//创建打印文件
		if("001".equals(mdata.getSoperCode())|| "002".equals(mdata.getSoperCode())|| "012".equals(mdata.getSoperCode()))
			KjggInfo.createWord(file, template, mdata,fund,principal,clob,peopleList,ssmbList,annex002List, servletContext.getRealPath("/"));
		if("005".equals(mdata.getSoperCode()))
			rkxInfo.createWord(file, template, mdata,fund,principal,clob,peopleList,ssmbList,annex002List, servletContext.getRealPath("/"));
		if("011".equals(mdata.getSoperCode()))
			KjcxInfo.createWord(file, template, mdata,fund,principal,clob,peopleList,ssmbList,annex002List,servletContext.getRealPath("/")); 
		if("009".equals(mdata.getSoperCode()))
			GjhzInfo.createWord(file, template, mdata,fund,principal,clob, gwGjhzunitList,gnGjhzunitList,GnList,GwList,ssmbList, servletContext.getRealPath("/"));
		if("006".equals(mdata.getSoperCode())||"007".equals(mdata.getSoperCode())||"008".equals(mdata.getSoperCode())||"013".equals(mdata.getSoperCode()))
			CyhInfo.createWord(file, template, mdata,fund,principal,clob,peopleList,ssmbList, annex002List,servletContext.getRealPath("/"));
		response.reset();
		
		//writeFileCommon(new File(file),docName+".doc",false);

		return "";
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<ItemAnnex> getAnnex002List() {
		return annex002List;
	}
	public void setAnnex002List(List<ItemAnnex> annex002List) {
		this.annex002List = annex002List;
	}
	
}
